/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.repository;

import com.yunqian.redot.domain.Ticket;
import com.yunqian.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 门票管理
 *
 * @author zsy
 * 2018-09-29 15:55:11
 */
@Repository
public interface TicketRepository extends BaseRepository<Ticket, Long> {

    /**
     * 根据展馆名称查找
     * @param title
     * @return
     */
    boolean existsByTitle(String title);

    /**
     * 根据展馆名称查找并排除id
     * @param title
     * @param id
     * @return
     */
    boolean existsByTitleAndIdNot(String title, Long id);

    /**
     * 根据门票类型查找列表
     * @param type
     * @return
     */
    List<Ticket> findAllByTypeOrderByIdAsc(Integer type);

    /**
     * 获取可用门票
     * @param status
     * @return
     */
    List<Ticket> findAllByStatus(Integer status);

    /**
     * 查找基础票
     * @param type
     * @return
     */
    List<Ticket> findAllByType(Integer type);

    /**
     * 判断是否是基础票
     * @param type
     * @param status
     * @param id
     * @return
     */
    boolean existsByTypeAndStatusAndId(Integer type, Integer status, Long id);

    /**
     * 根据展区字符串查找
     * @param str
     * @return
     */
    Ticket findFirstByRelateTicketArr(String str);

    /**
     * 减少库存
     * @param id
     * @param quantity
     */
    @Modifying
    @Query(value = "update t_ticket set stock = stock - :quantity where id = :id",nativeQuery = true)
    void subtractStock(@Param("id") Long id, @Param("quantity") Integer quantity);

    /**
     * 增加库存
     * @param id
     * @param quantity
     */
    @Modifying
    @Query(value = "update t_ticket set stock = stock + :quantity where id = :id",nativeQuery = true)
    void addStock(@Param("id") Long id, @Param("quantity") Integer quantity);
}
